package com.topscomm.mpm.script;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.BusinessException;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.mpm.MpmScript;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.*;
import com.topscomm.mpm.service.*;
import com.topscomm.mpm.service.impl.*;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wudaliang
 * @date 2021/1/11
 * @description:
 * @modify:
 */
public class MpmProjectBidScoreFeedBackScript extends MpmScript {


    /**
     * @author: hdy
     * @date: 2022-05-25
     * @description:
     * @return: void
     * @modify:
     */
    @Transactional
    public void projectScoreFeedBackAfterApproval(ITapService service, TapEntity entity)throws Exception{
    	// 审批点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String id = ConvertUtil.convertToString(nodeins.getSourceid());
        String state = ConvertUtil.convertToString(nodeins.getState());
        MpmProjectBidScoreFeedBackService mpmProjectBidScoreFeedBackService= (MpmProjectBidScoreFeedBackService)SpringUtil.getBean("mpmProjectBidScoreFeedBackService");
        MpmProjectBidScoreService mpmProjectBidScoreService= (MpmProjectBidScoreService)SpringUtil.getBean("mpmProjectBidScoreService");
		MpmProjectBidScoreFeedBackEntity bidScoreFeedBackEntity = mpmProjectBidScoreFeedBackService.queryById(id);
		if(bidScoreFeedBackEntity == null) {
			return;
		}
		String sourceid = ConvertUtil.convertToString(bidScoreFeedBackEntity.getSourceid());
		String completion = bidScoreFeedBackEntity.getCompletion();
		String completionscore = bidScoreFeedBackEntity.getCompletionscore();
		String improvescore = bidScoreFeedBackEntity.getImprovescore();
		// 如果审批通过，更新评标主表
		if( "2".equals(state)) {
			MpmProjectBidScoreEntity mpmProjectBidScoreEntity = mpmProjectBidScoreService.queryById(sourceid);
			mpmProjectBidScoreEntity.setCompletion(completion);
			mpmProjectBidScoreEntity.setCompletionscore(completionscore);
			mpmProjectBidScoreEntity.setImprovescore(improvescore);
			mpmProjectBidScoreService.updateEntity(mpmProjectBidScoreEntity);
		}
    }

	/**
	 * @author: houyuhang
	 * @date: 2022-11-28
	 * @description:
	 * @return: void
	 * @modify:
	 */
	@Transactional
	public void bidApprovalUser(ITapService service, TapEntity entity) throws Exception{
		// 审批点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String id = ConvertUtil.convertToString(nodeins.getSourceid());
		MpmProjectBidScoreFeedBackService mpmProjectBidScoreFeedBackService= (MpmProjectBidScoreFeedBackService)SpringUtil.getBean("mpmProjectBidScoreFeedBackService");
		MpmProjectBidScoreService mpmProjectBidScoreService= (MpmProjectBidScoreService)SpringUtil.getBean("mpmProjectBidScoreService");
		MpmProjectService mpmProjectService = (MpmProjectService)SpringUtil.getBean("mpmProjectService");
		MpmProjectBidScoreFeedBackEntity bidScoreFeedBackEntity = mpmProjectBidScoreFeedBackService.queryById(id);
		if(bidScoreFeedBackEntity == null) {
			return;
		}
		MpmProjectBidScoreEntity bidScoreEntity = mpmProjectBidScoreService.queryById(ConvertUtil.convertToString(bidScoreFeedBackEntity.getSourceid()));
		if(bidScoreEntity == null) {
			return;
		}
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled = 1 and projectcode = '{0}' ", bidScoreFeedBackEntity.getProjectcode());
		MpmProjectEntity mpmProjectEntity = mpmProjectService.queryObjectByWhere(sql.toString());
		sql.clear();
		sql.appendSingle(" state = 9 and projectcode = '{0}' and isfinishbidscore = '1' ", bidScoreFeedBackEntity.getProjectcode());
		List<Map<String, Object>> mpmProjectBidScoreFeedBackEntities = mpmProjectBidScoreFeedBackService.queryMapByWhere(sql.toString(), "feedbackBidFinish");
		sql.clear();
		sql.appendSingle(" state = 9 and projectcode = '{0}' ", bidScoreFeedBackEntity.getProjectcode());
		List<MpmProjectBidScoreEntity> mpmProjectBidScoreEntities = mpmProjectBidScoreService.queryByWhere(sql.toString());
		// 如果是最后一次评分细则反馈，则增加投标部人员审批
		if (mpmProjectBidScoreFeedBackEntities.size() + 1 == mpmProjectBidScoreEntities.size() && (MpmSystemConst.ProjectLevel.A.equals(mpmProjectEntity.getProjectlevel()) || MpmSystemConst.ProjectLevel.B.equals(mpmProjectEntity.getProjectlevel()))) {
			bidScoreFeedBackEntity.setBidapproval("1");
			List<String> bidUserList = mpmProjectBidScoreFeedBackService.queryBidUserForBidScore(mpmProjectEntity);
			bidScoreFeedBackEntity.setBidapprovaluser(bidUserList.stream().collect(Collectors.joining(",")));
			mpmProjectBidScoreFeedBackService.updateEntity(bidScoreFeedBackEntity);
			bidScoreEntity.setCompletionscore(bidScoreFeedBackEntity.getCompletionscore());
			mpmProjectBidScoreService.updateEntity(bidScoreEntity);
		}
	}
}
